package cn.eli.vue.controller;

import cn.eli.vue.api.CommonResult;
import cn.eli.vue.entity.*;
import cn.eli.vue.service.postAnswer.PostAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :lzs
 * @Date : 2021年7月16日
 * @Description : 回答业务控制器
 */

@Api("回答模块")
@ResponseBody
@RestController
public class PostAnswerBusinessController {
    @Autowired
    PostAnswerService postAnswerService;

    /*********************************          业务接口    ***********************************************/
    /**
     * 发布回答接口
     * 收取数据包：新发布的回答数据
     * 传回数据包：影响sql行数
     */
    @ApiOperation("发布回答")
    @RequestMapping(value = "/postanswer/answerpost",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerPost(@RequestBody PostAnswer postAnswer){
        int user_id = postAnswer.getUser_id();
        int postAsk_id =postAnswer.getPostAsk_id();
        String postAnswer_title = postAnswer.getPostAnswer_title();
        String postAnswer_discribe = postAnswer.getPostAnswer_discribe();
        String postAnswer_img = postAnswer.getPostAnswer_img();

        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAsk_id==0){
            return CommonResult.validateFailed("无对应的问题的信息");
        }
        /*也许回答的问题可以是空？*/
        if(postAnswer_title==null||postAnswer_title.equals("")){
            postAnswer_title="";
        }
        if(postAnswer_discribe==null||postAnswer_discribe.equals("")){
            return CommonResult.validateFailed("回答的正文内容不可为空");
        }
        if(postAnswer_img==null||postAnswer_img.equals("")){
            postAnswer_img="";
        }

        int postAnswer_res = postAnswerService.AnswerPost(user_id,postAsk_id,postAnswer_title,postAnswer_discribe,postAnswer_img);
        if(postAnswer_res != 0 && postAnswer_res != Integer.MIN_VALUE + 1001){
            return CommonResult.success("发布成功");
        }
        else{
            return CommonResult.validateFailed("发布失败");
        }
    }

    /**
     * 收藏回答接口
     * 收取数据包：
     * 传回数据包：影响sql行数
     */
    @ApiOperation("收藏回答")
    @RequestMapping(value = "/postanswer/answersubscribe",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerSubscribe(@RequestBody SubscribeAnswer subscribeAnswer){
        int user_id = subscribeAnswer.getUser_id() ;
        int postAnswer_id = subscribeAnswer.getPostAnswer_id();
        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAnswer_id==0){
            return CommonResult.validateFailed("无对应的回答的信息");
        }

        int answerSubscribeInsert_res = postAnswerService.AnswerSubscribeInsert(user_id,postAnswer_id);
        int answerSubscribeUpdate_res = postAnswerService.AnswerSubscribeUpdate(user_id,postAnswer_id);

        if(answerSubscribeInsert_res!=0&&answerSubscribeUpdate_res!=0){
            return CommonResult.success("收藏成功！");
        }
        else if(answerSubscribeInsert_res==0){
            return CommonResult.validateFailed("插入失败");
        }
        else if(answerSubscribeUpdate_res==0){
            return CommonResult.validateFailed("更新数据失败");
        }
        else {
            return CommonResult.validateFailed("未知错误");
        }
    }

    /**
     * 取消收藏回答接口
     * 收取数据包：
     * 传回数据包：影响sql行数
     */
    @ApiOperation("取消收藏回答")
    @RequestMapping(value = "/postanswer/answersubscribeCancel",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerSubscribeCancel(@RequestBody SubscribeAnswer subscribeAnswer){
        int user_id = subscribeAnswer.getUser_id() ;
        int postAnswer_id = subscribeAnswer.getPostAnswer_id();
        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAnswer_id==0){
            return CommonResult.validateFailed("无对应的回答的信息");
        }

        int answerSubscribeCancelDelete_res = postAnswerService.AnswerSubscribeCancelDelete(user_id,postAnswer_id);
        int answerSubscribeCancelUpdate_res = postAnswerService.AnswerSubscribeCancelUpdate(user_id,postAnswer_id);

        if(answerSubscribeCancelDelete_res!=0&&answerSubscribeCancelUpdate_res!=0){
            return CommonResult.success("收藏取消成功！");
        }
        else if(answerSubscribeCancelDelete_res==0){
            return CommonResult.validateFailed("删除失败");
        }
        else if(answerSubscribeCancelUpdate_res==0){
            return CommonResult.validateFailed("更新数据失败");
        }
        else {
            return CommonResult.validateFailed("未知错误");
        }
    }

    /**
     * 收藏点赞接口
     * 收取数据包：
     * 传回数据包：影响sql行数
     */
    @ApiOperation("点赞回答")
    @RequestMapping(value = "/postanswer/answerlike",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerLike(@RequestBody LikeAnswer likeAnswer){
        int user_id = likeAnswer.getUser_id() ;
        int postAnswer_id = likeAnswer.getPostAnswer_id();
        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAnswer_id==0){
            return CommonResult.validateFailed("无对应的回答的信息");
        }

        int answerLikeInsert_res = postAnswerService.AnswerLikeInsert(user_id,postAnswer_id);
        int answerLikeUpdate_res = postAnswerService.AnswerLikeUpdate(user_id,postAnswer_id);

        if(answerLikeInsert_res!=0&&answerLikeUpdate_res!=0){
            return CommonResult.success("点赞成功！");
        }
        else if(answerLikeInsert_res==0){
            return CommonResult.validateFailed("插入失败");
        }
        else if(answerLikeUpdate_res==0){
            return CommonResult.validateFailed("更新数据失败");
        }
        else {
            return CommonResult.validateFailed("未知错误");
        }
    }

    /**
     * 取消点赞回答接口
     * 收取数据包：
     * 传回数据包：影响sql行数
     */
    @ApiOperation("取消点赞")
    @RequestMapping(value = "/postanswer/answerlikecancel",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerLikeCancel(@RequestBody LikeAnswer likeAnswer){
        int user_id = likeAnswer.getUser_id() ;
        int postAnswer_id = likeAnswer.getPostAnswer_id();
        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAnswer_id==0){
            return CommonResult.validateFailed("无对应的回答的信息");
        }

        int answerLikeCancelDelete_res = postAnswerService.AnswerLikeCancelDelete(user_id,postAnswer_id);
        int answerLikeCancelUpdate_res = postAnswerService.AnswerLikeCancelUpdate(user_id,postAnswer_id);

        if(answerLikeCancelDelete_res!=0&&answerLikeCancelUpdate_res!=0){
            return CommonResult.success("点赞取消成功！");
        }
        else if(answerLikeCancelDelete_res==0){
            return CommonResult.validateFailed("删除失败");
        }
        else if(answerLikeCancelUpdate_res==0){
            return CommonResult.validateFailed("更新数据失败");
        }
        else {
            return CommonResult.validateFailed("未知错误");
        }
    }

    @ApiOperation("评论回答")
    @RequestMapping(value = "/postanswer/answercomment",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerComment(@RequestBody CommentAnswer commentAnswer){
        int user_id = commentAnswer.getUser_id();
        int postAnswer_id = commentAnswer.getPostAnswer_id();
        String commentAnswer_discribe = commentAnswer.getCommentAnswer_discribe();

        if(user_id==0){
            return CommonResult.validateFailed("无指定用户信息");
        }
        if(postAnswer_id==0){
            return CommonResult.validateFailed("无对应的问题的信息");
        }

        int answerCommentInsert_res  = postAnswerService.AnswerCommentInsert(user_id,postAnswer_id,commentAnswer_discribe);
        int answerCommentUpdate_res = postAnswerService.AnswerCommentUpdate(user_id,postAnswer_id,commentAnswer_discribe);

        if(answerCommentInsert_res!=0&&answerCommentUpdate_res!=0){
            return CommonResult.success("评论成功！");
        }
        else if(answerCommentInsert_res==0){
            return CommonResult.validateFailed("插入失败");
        }
        else if(answerCommentUpdate_res==0){
            return CommonResult.validateFailed("更新数据失败");
        }
        else {
            return CommonResult.validateFailed("未知错误");
        }
    }

    @ApiOperation("修改回答")
    @RequestMapping(value = "/postanswer/answermodify",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult answerModify(@RequestBody PostAnswer postAnswer ){
        int user_id = postAnswer.getUser_id();
        int postAnswer_id= postAnswer.getPostAnswer_id();
        String postAnswer_title = postAnswer.getPostAnswer_title();
        String postAnswer_discribe = postAnswer.getPostAnswer_discribe();
        String postAnswer_img = postAnswer.getPostAnswer_img();

        if(postAnswer_img==null||postAnswer_img.equals("")){
            postAnswer_img="";
        }

        int answerModify_res = postAnswerService.AnswerModify(user_id,postAnswer_id,postAnswer_title,postAnswer_discribe,postAnswer_img);

        if(answerModify_res!=0){
            return CommonResult.success("修改成功");
        }
        else{
            return CommonResult.validateFailed("修改失败");
        }
    }

    @ApiOperation("获取所有回答")
    @RequestMapping(value = "/postanswer/allanswer",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allAnswer(){
        List<PostAnswer> allAnswer_res = postAnswerService.AllAnswer();

        if(allAnswer_res!=null){
            return CommonResult.success(allAnswer_res,"获取成功");
        }
        else{
            return CommonResult.validateFailed("获取失败或是暂无回答");
        }
    }

    @ApiOperation("获取一个问题下的所有回答")
    @RequestMapping(value = "/postanswer/allanswerinask",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allAnswerInAsk(@RequestBody PostAnswer postAnswer){
        int postAsk_id = postAnswer.getPostAsk_id();

        List<PostAnswer> allAnswerInAsk_res = postAnswerService.AllAnswerInAsk(postAsk_id);

        if(allAnswerInAsk_res!=null){
            return CommonResult.success(allAnswerInAsk_res,"获取成功");
        }
        else{
            return CommonResult.validateFailed("获取失败或是暂无回答");
        }
    }

    @ApiOperation("获取一个用户发布的所有回答")
    @RequestMapping(value = "/postanswer/allanswerinuser",method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult allAnswerInUser(@RequestBody PostAnswer postAnswer){
        int user_id = postAnswer.getUser_id();

        List<PostAnswer> allAnswerInUser_res = postAnswerService.AllAnswerInUser(user_id);

        if(allAnswerInUser_res!=null){
            return CommonResult.success(allAnswerInUser_res,"获取该用户的回答成功");
        }
        else{
            return CommonResult.success("该用户无回答");
        }
    }
}
